<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\member\controller\admin;

use manniu\controller\AdminBase;

use app\member\model\Member;
use app\member\model\MemberGroup;

class Index extends AdminBase
{

    //当前控制器菜单
    public $submenu = [];

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->submenu[] = ['title' => '会员管理', 'url' => url('member/admin.index/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '会员添加', 'url' => url('member/admin.index/add'), 'icon' => '&#xe654;'];
    }

    /**
     * 后台会员管理首页
     */
    public function index($group_id = 0, Member $member)
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action == 'del') {
                $ids = $this->request->param('ids/a');
                if (empty($ids)) {
                    return mn_ajax_msg(0, '你还没有选择呢');
                }
                $member->del($ids);
                return mn_ajax_msg(1, '删除成功');
            } elseif ($action == 'move') {
                $ids = $this->request->param('ids/a');
                $g_groupid = (int)$this->request->param('g_groupid');
                if (empty($ids)) {
                    return mn_ajax_msg(0, '你还没有选择呢');
                }
                $list = [];
                foreach ($ids as $key => $id) {
                    $list[] = ['uid' => (int)$id, 'group_id' => $g_groupid];
                }
                $member->saveAll($list);
                return mn_ajax_msg(1, '会员组移动成功');
            } else {
                return mn_ajax_msg(0, '未知操作');
            }
        }
        $group_id = (int)$group_id;
        if ($group_id) {
            $member->where('group_id', $group_id);
        }

        $list = $member->order('uid', 'desc')->paginate(config('mn_admin.admin_pagesize'));
        $this->assign([
            'list' => $list,
            'groups' => MemberGroup::getGroupAll(),
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add(Member $member)
    {
        if ($this->request->isPost()) {
            $post_data = $this->request->post();
            $this->checkUnique($post_data);
            $uid = $member->register($post_data, true);
            if (is_numeric($uid)) {
                $this->success('会员[' . $member->username . ' : ' . $member->uid . ']添加成功', 'index');
            } else {
                $this->error($member->getError());
            }
        }

        $this->assign([
            'groups' => MemberGroup::getGroupAll(),
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 修改
     */
    public function edit($uid)
    {
        $uid = (int)$uid;
        if (!$uid) {
            $this->error('缺少会员ID');
        }
        $member = Member::get($uid);
        if (!$member) {
            $this->error('此会员不存在', 'index');
        }
        if ($this->request->isPost()) {
            $post_data = $this->request->post();
            $this->checkUnique($post_data, $uid);
            if (false !== $member->allowField(true)->validate('Member.edit')->save($post_data, ['uid' => $uid])) {
                $this->success('修改成功', 'index');
            } else {
                $this->error($member->getError());
            }
        }
        $this->assign([
            'groups' => MemberGroup::getGroupAll(),
            'data' => $member->getData(),
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 验证参数唯一性
     * @param $post_data
     */
    public function checkUnique($post_data, $uid = 0)
    {
        $member = new Member();
        if($uid){
            $member->where('uid', '<>', $uid);
        }
        foreach ($post_data as $key => $value) {
            //验证昵称是否重复
            if (!empty($value) && $key == 'nickname') {
                if ($member->where('nickname', $value)->count()) {
                    $this->error('昵称已存在');
                }
            }
            //验证手机号是否重复
            if (!empty($value) && $key == 'phone') {
                if ($member->where('phone', $value)->count()) {
                    $this->error('手机号码已被使用');
                }
            }
            //验证邮箱是否重复
            if (!empty($value) && $key == 'email') {
                if ($member->where('email', $value)->count()) {
                    $this->error('邮箱已被使用');
                }
            }
        }
    }
} 